/**
 * 派工信息列表
 */
$(function() {
    showArrangeInfo();
    function showArrangeInfo() {
        layui.use(['table','form','element','laydate'], function(){
            var $=layui.$;
            var table = layui.table;
            var form = layui.form;
            var element = layui.element;
            var laydate = layui.laydate;

            console.log("进入ArrangeInfo.js主页面");
            // 年月日选择器渲染
            laydate.render({
                elem: '#serv_start_date'
            });
            laydate.render({
                elem: '#serv_due_date'
            });
            //时间选择器
            laydate.render({
                elem: '#serv_start_time'
                ,type: 'time'
                ,format: 'HH:mm'
            });

            //数据表格渲染
            var tableIns = table.render({
                elem: '#arrangeInfoList'
                , id: 'arrangeInfo'
                , url: '/arrangeInfo/showArrangeInfo'
                // , toolbar:true
                // , totalrow:true
                , height: 420
                , cellMinWidth: 100//全局定义常规单元格的最小宽度，layui 2.2.1 新增
                , request: {
                    pageName: 'pn' //页码的参数名称，默认：page
                    , limitName: 'limit' //每页数据量的参数名，默认：limit
                }
                , parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                    return {
                        "code": 0, //解析接口状态
                        "msg": '', //解析提示文本
                        "count": res.total, //解析数据长度
                        "data": res.list //解析数据列表
                    }
                }
                ,cols: [[
                    // {type:'radio'}
                    {field: 'serv_org', width: 200, title: '服务机构',sort:true}
                    , {field: 'settle_code', width: 200,title: '结算代码'}
                    , {field: 'arrange_time', width:200,title: '派工单制定时间',sort:true,templet : "<div>{{layui.util.toDateString(d.arrange_time, 'yyyy年MM月dd日 HH:mm:ss')}}</div>"}
                    , {field: 'nursing_yb_id', width: 150, title: '护工医保ID'}
                    , {field: 'nursing_name', width: 100, title: '护工姓名'}
                    , {field: 'nursing_id', width: 200, title: '护工身份证号'}
                    , {field: 'elder_yb_id', width: 150, title: '老人医保ID'}
                    , {field: 'elder_name', width: 100, title: '老人姓名'}
                    , {field: 'elder_id', width: 200,title: '老人身份证号'}
                    , {field: 'serv_start_date', width: 150,title: '护理开始日期',sort: true}
                    , {field: 'serv_due_date', width: 150,title: '护理结束日期', sort: true}
                    , {field: 'chosen_weekday', width:200,title: '每周的工作日'}
                    , {field: 'serv_monday', width: 150, title: '周一'}
                    , {field: 'serv_tuesday', width: 150, title: '周二'}
                    , {field: 'serv_wednesday', width: 150,title: '周三'}
                    , {field: 'serv_thursday', width: 150,title: '周四'}
                    , {field: 'serv_friday', width: 150,title: '周五'}
                    , {field: 'serv_saturday', width: 150,title: '周六'}
                    , {field: 'serv_sunday', width: 150, title: '周日'}
                    , {field: 'serv_content', width: 200, title: '护理内容'}
                    , {field: 'serv_status', width: 100, title: '状态'}
                    , {field: 'first_plan_date', width: 200, title: '护理计划首次制定日期'}
                    , {field: 'first_serve_date', width:200,title: '首次上门服务日期'}
                    , {fixed: 'right', toolbar: '#Check_Edit_Del', width: 170, title: '操作'}
                ]]
                , page: true
            });

            //根据时间更新订单状态
            $.ajax({
                method:"POST"
                ,url: "/arrangeInfo/judgeStatus"
                ,success: function(res){
                    console.log("成功");
                    table.reload('arrangeInfo', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                    });
                }
                ,error:function (data) {
                    console.log("提交失败");
                    layer.msg("提交失败",{ icon: 5,anim: 6,offset: "auto"});
                }
            });

            // 搜索/增加选项卡 功能
            var $ = layui.$, active = {
                // 派工信息页面-搜索功能
                reload1: function(){
                    console.log("触发搜索按钮");
                    var send_servOrg = $('#serv_org');
                    var send_settleCode = $('#settle_code');
                    var send_servStatus=$('#serv_status');
                    var send_elderName = $('#elder_name');
                    var send_elderYbid = $('#elder_yb_id');
                    var send_elderId=$('#elder_id');
                    var send_nursingName = $('#nursing_name');
                    var send_nursingYbid= $('#nursing_yb_id');
                    var send_nursingId=$('#nursing_id');
                    var send_startDate = $('#serv_start_date');
                    var send_dueDate = $('#serv_due_date');
                    var send_startTime=$('#serv_start_time');
                    console.log("搜索的机构是："+send_servOrg.val());
                    console.log("搜索的结算编码是："+send_settleCode.val());
                    console.log("搜索的状态是："+send_servStatus.val());

                    //执行重载
                    table.reload('arrangeInfo', {
                        url: '/arrangeInfo/searchArrangeInfo',
                        method:'post',
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                        ,where: {
                            servOrg:send_servOrg.val(),
                            settleCode:send_settleCode.val(),
                            servStatus:send_servStatus.val(),
                            elderName:send_elderName.val(),
                            elderYbid:send_elderYbid.val(),
                            elderId:send_elderId.val(),
                            nursingName:send_nursingName.val(),
                            nursingYbid:send_nursingYbid.val(),
                            nursingId:send_nursingId.val(),
                            startDate:send_startDate.val(),
                            dueDate:send_dueDate.val(),
                            startTime:send_startTime.val(),
                        }
                    }, 'data');
                },
                tabAdd: function(data){
                    //新增一个Tab项
                    console.log("触发tabAdd按钮");
                    console.log();
                    element.tabAdd('demo', {
                        title: '制定派工订单'
                        ,content:'<iframe src="/arrangeInfo/toAddArrangeInfo" style="height:2000px!important;width: 100%;border:none"></iframe>'
                        ,id:'addArangeInfo' //实际使用一般是规定好的id，这里以时间戳模拟下
                    });
                    element.tabChange('demo', 'addArangeInfo');
                }
            };
            $('.SearchLoadArrangeInfo .layui-btn').on('click', function () {
                console.log("触发按键");
                var type = $(this).data('type');
                console.log("type:"+type);
                active[type] ? active[type].call(this) : '';
                console.log("结束");
            });

            //派工页面的监听（查看\排程\删除）
            table.on('tool(arrangeInfo)', function(obj){
                var data = obj.data;
                console.log("输出data",data);
                var data2=$('#arrangeDetail')[0].innerHTML.format(data);
                if(obj.event === 'detail'){
                    console.log("显示详细页面");
                    layer.open({
                        type: 1,
                        skin: 'layui-layer-demo', //样式类名
                        title: ['基本信息', 'font-size:20px'],
                        closeBtn: 1,
                        area: ['700px', '500px'],
                        shadeClose: true, //开启遮罩关闭
                        btn: ['关闭'],
                        content:data2
                    });
                }
                else if(obj.event === 'del'){
                    layer.confirm('是否确认删除？', function(index){
                        $.ajax({
                            url:'/arrangeInfo/deleteArrangeOrder',
                            method:'post',
                            data:{id:data.id},
                            success:function(){
                                console.log("删除派工订单成功");
                                obj.del();
                                layer.close(index);
                            },
                            error:function(){
                                layer.msg("删除派工订单出错")
                            }
                        })
                    });
                }

                else if(obj.event === 'arrange'){
                    console.log("ArrangeInof主页面触发排程按键");
                    window.parent.document.getElementById('transferElderYbid').value=data.elder_yb_id;
                    window.parent.document.getElementById('transferArrangeID').value=data.id;
                    window.parent['dateArrange'].location.reload();
                    element.tabChange('demo','arrangeDate');
                }
            });
        })
    }
});


